﻿@model Shop.Models.Product

@{
    ViewBag.Title = "Create";
    Layout = "~/Areas/Admin/Views/Shared/_LayoutForm.cshtml";
}

@using (Ajax.BeginForm("Create", new AjaxOptions()
{
    OnSuccess = "onSuccess"
}))
{
    @Html.AntiForgeryToken()

    @Html.HiddenFor(model=>model.Id)

    <div class="form-horizontal">
        <div class="form-group">
            @Html.LabelFor(model => model.CategrayId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.CategrayId, (SelectList)ViewBag.Cate, new { @class = "form-control" })

                @Html.ValidationMessageFor(model => model.CategrayId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LowPrice, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LowPrice, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LowPrice, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DefaultImage, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" name="file" id="file" value="" class="form-control" />
                <button class="btn btn-default" id="btnUpload">上传</button>
                <img id="upImg" src="@Model.DefaultImage" alt="Alternate Text" style="width:100px" />
                <input type="hidden" id="DefaultImage" name="DefaultImage" value="@Model.DefaultImage" />
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Brand, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Brand, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Brand, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Remark, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <textarea id="editor" name="Remark">@Model.Remark</textarea>
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="提交" class="btn btn-default" />
            </div>
        </div>
    </div>
}



@section scripts{
    <script src="~/Scripts/jquery-1.12.4.js"></script>
    <script src="~/UEditor/ueditor.config.js"></script>
    <script src="~/UEditor/editor_api.js"></script>
    <script src="~/UEditor/lang/zh-cn/zh-cn.js"></script>

    <script>



        function onSuccess(res) {
            if (res.success) {
                parent.closeModal(); // 调用父窗口中的方法用来关闭窗口、刷新数据
            } else {
                alert(res.msg)
            }
        }


        $(function () {
            let ue= UE.getEditor('editor')

            
            // 上传按钮
            $("#btnUpload").click(function () {
                if ($('#file').val()) {
                    var formdata = new FormData();
                    formdata.append('file', $('#file')[0].files[0])
                    $.ajax({
                        url: '/upload',
                        type: 'post',
                        data: formdata,
                        contentType: false,
                        processData: false,
                        success: function (res) {
                            console.log(res);
                            $('#upImg').attr('src', res.data)
                            $('#DefaultImage').val(res.data)
                        }
                    })
                } else {
                    alert('请选择文件')
                }
                return false;
            })
        })
    </script>

}

